Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

225
Vistas
Why won't my stringify parse in an "if" statement?
const friends = ["john","peter","bob"];
localStorage.setItem("friends", JSON.stringify(friends));

const values = JSON.parse(localStorage.getItem("friends"));
console.log(values[2]);
// console.log(values[1]);
// console.log(values[2]);

let fruits;
console.log(fruits);

if (localStorage.getItem('fruits')) {
 fruits = JSON.parse(localStorage.getItem('fruits'));
}
else {
 fruits = [];
}

console.log(fruits);
fruits.push("apple");
fruits.push("orange");
localStorage.setItem("fruits", JSON.stringify(fruits));

Why will the first array behave properly in chrome where I can clearly check the console and application from dev.tools but when I try to make an empty array and push different values fx "apple" and "orange" I can't. My console says the error is in the "if" statement, but I don't understand why. Any thoughts?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I believe the answer lies in the quotes. JSON syntax is very strict and requires the use of double quotes. Change out all your single quotes in your if statement to double quotes and see if that fixes your issue.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda